﻿using System;
using System.IO;
using System.Xml.Serialization;
using LoLTimers.DataObjects;

namespace LoLTimers
{
    public sealed class Settings
    {
        private string _settingsPath;

        private SettingsDataObject _settingsDataObject = new SettingsDataObject();

        public SettingsDataObject SettingsData
        {
            get { return _settingsDataObject; }
        }


        public Exception LoadSettingsXml(string settingsPath)
        {
            _settingsPath = settingsPath;
            try
            {
                XmlSerializer settingsDeSerializer = new XmlSerializer(typeof(SettingsDataObject));
                TextReader textReader = new StreamReader(_settingsPath);
                _settingsDataObject = (SettingsDataObject)settingsDeSerializer.Deserialize(textReader);
                textReader.Close();

                //Pair the tdo's with the gdo's
                foreach (TimerDataObject timerDataObject in _settingsDataObject.TimerDataObjects)
                {
                    foreach (GameDataObject gameDataObject in _settingsDataObject.GameDataObjects)
                    {
                        if (gameDataObject.ID == timerDataObject.TimerType)
                        {
                            timerDataObject.GameData = gameDataObject;
                            break;
                        }
                    }
                }

            }
            catch (Exception ex)
            {
                return ex;
            }
            return null;
        }

        public Exception SaveSettingsXml()
        {
            _settingsDataObject.UpdateTimerInterval = TimerManager.Instance.GetUpdateTimerInterval();
            //_settingsDataObject.TimerDataObjects.Clear();
            //foreach (TimerDisplay timerDisplay in TimerManager.Instance.TimerDisplays)
            //{
            //    TimerDataObject tmpTDO = new TimerDataObject();
            //    tmpTDO.ID = timerDisplay.ID;
            //    tmpTDO.TimerType = timerDisplay.
            //    _settingsDataObject.TimerDataObjects.Add();
            //}

            try
            {
                XmlSerializer settingsSerializer = new XmlSerializer(typeof(SettingsDataObject));

                TextWriter textWriter = new StreamWriter(_settingsPath);
               
                settingsSerializer.Serialize(textWriter, _settingsDataObject);

                textWriter.Close();
            }
            catch (Exception ex)
            {
                return ex;
            }
            return null;
        }

       
        
        #region Singleton Crap
        private static volatile Settings _instance;
        private static object _syncRoot = new object();
        
        private Settings()
        {
        }
        
        public static Settings Instance
        {
            get
            {
                if(_instance == null)
                {
                    lock (_syncRoot)
                    {
                        if(_instance == null)
                            _instance = new Settings();
                    }
                }
                return _instance;
            }
        }
        #endregion
    }
}
